package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	import com.sina.microblog.utils.StringEncoders;
	import com.ut.nm_test_internal;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.utils.ByteArray;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNull;
	import org.flexunit.async.Async;
	
	use namespace nm_test_internal;
	public class PostStatusesTest
	{
		private static const STATUS_SHOW_URL_BASE:String = "http://api.t.sina.com.cn/statuses/show/";
		private static const STATUS_UPDATE_URL_BASE:String = "http://api.t.sina.com.cn/statuses/update.xml";
		private static const STATUS_UPLOAD_URL_BASE:String = "http://api.t.sina.com.cn/statuses/upload.xml";
		private static const STATUS_DESTROY_URL_BASE:String = "http://api.t.sina.com.cn/statuses/destroy/";
		private static const STATUS_REPOST_URL_BASE:String = "http://api.t.sina.com.cn/statuses/repost.xml";
		private static const STATUS_COMMENT_URL_BASE:String = "http://api.t.sina.com.cn/statuses/comment.xml";
		private static const STATUS_COMMENT_DESTROY_URL_BASE:String = "http://api.t.sina.com.cn/statuses/comment_destroy/";
		private static const STATUS_REPLY_URL_BASE:String = "http://api.t.sina.com.cn/statuses/reply.xml";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;
		private static var tester:String = "1231232";
		private static var origMessage:String = "测试消息，&消息bca==";
		private static var testMessage:String = StringEncoders.urlEncodeUtf8String(origMessage);
		private static var testMessageID:uint = 132323;
		
		public function PostStatusesTest()
		{
		}
		
		[BeforeClass]
		public static function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
			}
		}
		
		//Test Status Show.
		[Test(async,timeout="20000")]
		public function testStatusShow():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_STATUS_INFO_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = STATUS_SHOW_URL_BASE + testMessageID.toString() + ".xml" + params.toGetString();
			
			var req:URLRequest = new URLRequest(url);
			mb.loadStatusInfo(testMessageID);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusShowFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_STATUS_INFO_ERROR, AssertResult.assertError, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = STATUS_SHOW_URL_BASE + testMessageID.toString() + ".xml" + params.toGetString();
			
			var req:URLRequest = new URLRequest(url);
			mb.loadStatusInfo(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		
		//Test Status update.
		[Test(async,timeout="20000")]
		public function testStatusUpdate():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.UPDATE_STATUS_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_UPDATE_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.status = testMessage;
			mb.updateStatus(origMessage);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusUpdateFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.UPDATE_STATUS_ERROR, AssertResult.assertError, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = STATUS_UPDATE_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.status = testMessage;
			mb.updateStatus(origMessage);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Test]
		public function testStatusUpdateWithNullStatus():void
		{
			mb.lastRequest = null;
			mb.updateStatus(null);
			assertNull(mb.lastRequest);
		}
		[Test]
		public function testStatusUpdateWithEmptyStatus():void
		{
			mb.lastRequest = null;
			mb.updateStatus("");
			assertNull(mb.lastRequest);
		}
		
		//Test Status update.
		[Test(async,timeout="20000")]
		public function testStatusUpload():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.UPDATE_STATUS_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var ba:ByteArray = new ByteArray();
			ba.writeUTFBytes("This is a test");
		
			var url:String = STATUS_UPLOAD_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			mb.updateStatus(origMessage, "test.jpg", ba);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusUploadFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.UPDATE_STATUS_ERROR, AssertResult.assertError, TIME_OUT);
			
			var ba:ByteArray = new ByteArray();
			ba.writeUTFBytes("This is a test");
			
			var url:String = STATUS_UPLOAD_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			mb.updateStatus(origMessage, "test.jpg", ba);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Test(async,timeout="20000")]
		public function testStatusUploadWithNullStatus():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.UPDATE_STATUS_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var ba:ByteArray = new ByteArray();
			ba.writeUTFBytes("This is a test");
			
			var url:String = STATUS_UPLOAD_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			mb.updateStatus(null, "test.jpg", ba);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test]
		public function testStatusUploadRequest():void
		{	
			
			var req:URLRequest = new URLRequest(STATUS_UPLOAD_URL_BASE);
			req.method = URLRequestMethod.POST;
			var destReq:URLRequest = mb.getUploadImageRequest(origMessage);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("status", testMessage);
			req.data = params.toPostData();
			
			assertEquals(req.url, destReq.url);
			assertEquals(req.method, destReq.method);
			assertEquals(req.data.source, destReq.data.source);
			assertEquals(req.data.status, destReq.data.status);
		}
		//Test Status Destroy.
		[Test(async,timeout="20000")]
		public function testStatusDestroy():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.DELETE_STATUS_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_DESTROY_URL_BASE + testMessageID.toString() + ".xml";
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.deleteStatus(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusDestroyFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.DELETE_STATUS_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_DESTROY_URL_BASE + testMessageID.toString() + ".xml";
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			
			mb.deleteStatus(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		
		//Test Status Repost.
		[Test(async,timeout="20000")]
		public function testStatusRepost():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.REPOST_STATUS_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_REPOST_URL_BASE;
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.id = testMessageID;
			req.data.status = testMessage;
			
			mb.repostStatus(testMessageID, origMessage);
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusRepostFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.REPOST_STATUS_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_REPOST_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.id = testMessageID;
			mb.repostStatus(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		
		//Test Status Comment.
		[Test(async,timeout="20000")]
		public function testStatusComment():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.COMMENT_STATUS_RESULT, AssertResult.assertComment, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_COMMENT_URL_BASE;
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.id = testMessageID;
			req.data.comment = testMessage;
			
			mb.commentStatus(testMessageID, origMessage);
			URLRequestChecker.check(mb, req, ResultDataUtils.comment);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusCommentFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.COMMENT_STATUS_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_COMMENT_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.id = testMessageID;
			req.data.cid = testMessageID;
			
			mb.commentStatus(testMessageID, null, testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		
		//Test Status Comment destroy.
		[Test(async,timeout="20000")]
		public function testStatusCommentDestroy():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.DELETE_COMMENT_RESULT, AssertResult.assertComment, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_COMMENT_DESTROY_URL_BASE+ testMessageID.toString() + ".xml";
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			
			mb.deleteComment(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.comment);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusCommentDestroyFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.DELETE_COMMENT_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_COMMENT_DESTROY_URL_BASE+ testMessageID.toString() + ".xml";
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			
			mb.deleteComment(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		
		//Test Status reply.
		[Test(async,timeout="20000")]
		public function testStatusReply():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.REPLY_STATUS_RESULT, AssertResult.assertComment, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_REPLY_URL_BASE;
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.id = testMessageID;
			req.data.comment = testMessage;
			req.data.cid = testMessageID;
			
			mb.replyStatus(testMessageID, origMessage, testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.comment);			
			
		}
		[Test(async,timeout="20000")]
		public function testStatusReplyFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.REPLY_STATUS_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = STATUS_REPLY_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.id = testMessageID;
			req.data.cid = testMessageID;
			
			mb.replyStatus(testMessageID, null, testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
	}
}